<?php
/**
 * Created by PhpStorm.
 * User: 魏爷
 * Date: 2016/5/12
 * Time: 20:07
 */

namespace Admin\Model;

class UserModel extends BaseModel
{
    // 数据表名（不包含表前缀）
    protected $tableName        =   'user';

    /**
     * 查询用户所有信息
     * @return array()
    **/
    public function getAllUserInfo()
    {
        return $this->where("u_del=1")->order('u_id DESC')->select();
    }
    /**
     * 查询用户与有用户类型表
     * @param String $u_name 用户名称
     * @param String $u_email 用户邮箱
     * @param Integer $u_type_name 用户类型名称
     * $return Array()
    **/
    public function getUserAndType($u_name, $u_email, $u_type_name)
    {
        $where=array();
        if (!empty($u_name)) {
            $where['u_name']= array('LIKE',"%{$u_name}%");
        }
        if (!empty($u_email)) {
            $where['u_email']=array('LIKE',"%{$u_email}%");
        }
        if (!empty($u_type_name)) {
            $where['u_type_name']=array('LIKE',"$u_type_name");
        }
        //        $where['u_del']=1;
        $array=M('user as u')->join('wyz_usertype as c ON u.u_type_id=c.u_type_id', 'LEFT')->where($where)->order('u_id DESC')->select();
        return $array;
    }
    /**
     * 查询用户与有用户类型表
     * @param String $u_name 用户名称
     * @param String $u_email 用户邮箱
     * @param Integer $u_type_name 用户类型名称
     * $return Array()
     **/
    public function getUserStatusInfo($u_name, $u_email, $u_type_name)
    {
        $where=array();
        if (!empty($u_name)) {
            $where['u_name']= array('LIKE',"%{$u_name}%");
        }
        if (!empty($u_email)) {
            $where['u_email']=array('LIKE',"%{$u_email}%");
        }
        if (!empty($u_type_name)) {
            $where['u_type_name']=array('LIKE',"$u_type_name");
        }
        $where['u_status']=3;
        $array=M('user as u')->join('wyz_usertype as c ON u.u_type_id=c.u_type_id', 'LEFT')->where($where)->order('u_id DESC')->select();
        return $array;
    }
    /**
     * 查询用户与用户类型表所有信息
     * @param Integer $u_id 用户ID
     * @return Array（）
    **/
    public function getUserInfo($u_id)
    {
        return M('user as u')->join('wyz_usertype as c ON u.u_type_id=c.u_type_id', 'LEFT')->where("u_id='$u_id'")->find();
    }
    /**
     * 删除用户
     * @param Integer $u_id 用户id
     * @return boolean
    **/
    public function readRecordDel($u_id)
    {
        $res=$this->where("u_id='$u_id'")->setField('u_del', '2');
        return $res == false ? false:true;
    }
    /**
     * 恢复用户
     * @param Integer $u_id 用户ID
     * @return boolean
    **/
    public function readRecordRec($u_id)
    {
        $res=$this->where("u_id='$u_id'")->setField('u_del', '1');
        return $res == false ? false:true;
    }
    /**
     * 驳回认证
     * @param Integer $u_id 用户id
     * @return boolean
     **/
    public function readRealDel($u_id)
    {
        $res=$this->where("u_id='$u_id'")->setField('u_status', '1');
        if ($res==false) {
            return false;
        } else {
            $this->where("u_id='$u_id'")->setField('real_name', 'null');
            $this->where("u_id='$u_id'")->setField('real_number', 'null');
            return true;
        }
    }
    /**
     * 同意认证
     * @param Integer $u_id 用户ID
     * @return boolean
     **/
    public function readRealRec($u_id)
    {
        $res=$this->where("u_id='$u_id'")->setField('u_status', '2');
        return $res == false ? false:true;
    }
    /**
     * 查询用户Email
     * @param String $u_email 用户邮箱
     * @return boolean
     **/
    public function getUserEmail($u_email)
    {
        $res= $this->where("u_email LIKE '$u_email'")->select();
        if ($res==false) {
            return false;
        } else {
            return true;
        }
    }
    /**
     * 添加用户
     * @param String $u_name 用户名称
     * @param String $u_pwd 用户密码
     * @param String $u_sex 用户性别
     * @param String $u_type_id 用户类型ID
     * @param String $u_tel 用户电话
     * @return boolean
    **/
    public function readRecordAdd($u_name, $u_pwd, $u_sex, $u_type_id, $u_tel)
    {
        $data['u_name']=$u_name;
        $data['u_pwd']=myMd5($u_pwd);
        $data['u_sex']=$u_sex;
        $data['u_type_id']=$u_type_id;
        $data['u_tel']=$u_tel;
        $data['u_regdate']=time();
        $data['u_status']=1;
        $data['u_del']=1;
        $res=$this->add($data);
        if ($res>0) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 读者档案读者类型修改
     * @param Integer $u_id 读者ID
     * @param Integer $u_type_id 读者类型ID
     * @return boolean
    **/
    public function readRecordTypeSave($u_id, $u_type_id)
    {
        $res=$this->where("u_id='$u_id'")->setField('u_type_id', $u_type_id);
        return $res == false ? false:true;
    }
    /**
     * 读者档案修改
     * @param Integer $u_id 读者ID
     * @param String $u_name 读者名称
     * @param String $u_email 读者邮箱
     * @param Integer $u_tel 读者电话
     * @param Integer $u_age 读者年龄
     * @param String $u_address 读者地址
     * @param Integer $u_postcode 读者邮编
     * @param Integer $u_sex 读者性别
     * @param String $pictrueName 读者头像
     * @return boolean
     **/
    public function readRecordInfoSave($u_id, $u_name, $u_tel, $u_age, $u_address, $u_postcode, $u_sex, $pictrueName)
    {
        $data['u_name']=$u_name;
        $data['u_tel']=$u_tel;
        $data['u_age']=$u_age;
        $data['u_address']=$u_address;
        $data['u_postcode']=$u_postcode;
        $data['u_sex']=$u_sex;
        if (empty($pictrueName)) {
            $res=$this->where("u_id='$u_id'")->save($data);
            if ($res>0) {
                return true;
            } else {
                return false;
            }
        } else {
            $data['u_pic']=$pictrueName;
            $res=$this->where("u_id='$u_id'")->save($data);
            if ($res>0) {
                return true;
            } else {
                return false;
            }
        }
    }
}
